package com.woniuxy.meal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.meal.common.ConstantData;
import com.woniuxy.meal.dao.StoreInfoMapper;
import com.woniuxy.meal.entity.FoodType;
import com.woniuxy.meal.entity.FoodVariety;
import com.woniuxy.meal.dao.FoodVarietyMapper;
import com.woniuxy.meal.entity.StoreInfo;
import com.woniuxy.meal.exception.FoodVarietyException.FoodVarietyException;
import com.woniuxy.meal.exception.FoodVarietyException.FoodVarietyExceptionCode;
import com.woniuxy.meal.exception.foodTypeException.FoodTypeException;
import com.woniuxy.meal.exception.foodTypeException.FoodTypeExceptionCode;
import com.woniuxy.meal.exception.storeInfoException.StoreInfoException;
import com.woniuxy.meal.exception.storeInfoException.StoreInfoExceptionCode;
import com.woniuxy.meal.form.FoodVarietySearchForm;
import com.woniuxy.meal.service.FoodTypeService;
import com.woniuxy.meal.service.FoodVarietyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.meal.service.StoreInfoService;
import com.woniuxy.meal.service.dto.FoodForUserDto;
import com.woniuxy.meal.service.dto.FoodVarietyDto;
import com.woniuxy.meal.service.dto.WoniuPageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author zz
 * @since 2023年06月16日
 */
@Service
public class FoodVarietyServiceImpl extends ServiceImpl<FoodVarietyMapper, FoodVariety> implements FoodVarietyService {
    @Autowired
    private FoodVarietyMapper mapper;
    @Autowired
    private StoreInfoService service;
    @Autowired
    private FoodTypeService typeService;
    @Override
    public WoniuPageDto searchByCondition(FoodVarietySearchForm form) {
        StoreInfo store = service.getById(form.getStoreId());
        if(store==null || store.getStatus()==ConstantData.STORE_STATE_CLOSE){
            throw new StoreInfoException(StoreInfoExceptionCode.STORE_IS_NOT_EXIST);
        }
        Page<FoodVariety> page = new Page<>(form.getCurrent() == null ? 1 : form.getCurrent(), form.getSize() == null ? 20 : form.getSize());
        QueryWrapper<FoodVariety> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", ConstantData.FOOD_NOT_DELETE).eq("state",1).eq("storeId", form.getStoreId()).orderByDesc("sortNum"); //按招牌排序
        if(form.getStartPrice()!=null){
            queryWrapper.ge("price",form.getStartPrice());
        }
        if(form.getEndPrice()!=null){
            queryWrapper.le("price",form.getEndPrice());
        }
        if(form.getFoodName()!=null){
            queryWrapper.like("foodName",form.getFoodName());
        }
        if(form.getTypeId()!=null){
            FoodType foodType = typeService.getById(form.getTypeId());
            if(foodType.getTypeState()!=ConstantData.FOOD_STATE_NORMAL){
                throw new FoodTypeException(FoodTypeExceptionCode.FOOD_TYPE_STATE_ERROR);
            }
            queryWrapper.eq("typeId",form.getTypeId());
        }
        Page<FoodVariety> foodVarietyPage = mapper.selectPage(page, queryWrapper);
        List<FoodForUserDto> dtos = new ArrayList<>();
        for (FoodVariety food : foodVarietyPage.getRecords()) {
            FoodForUserDto dto = BeanUtil.toBean(food, FoodForUserDto.class);
            //----------------------
            dto.setCount(1); //月销量
            //----------------------
            dtos.add(dto);
        }
        WoniuPageDto pageDto = BeanUtil.toBean(page, WoniuPageDto.class);
        pageDto.setList(dtos);
        return pageDto;
    }

    @Override
    public FoodVarietyDto getFoodInfo(Long id) {
        FoodVariety foodVariety = mapper.selectById(id);
        if(foodVariety.getDeleted()==ConstantData.FOOD_DELETE){
            throw new FoodVarietyException(FoodVarietyExceptionCode.FOOD_IS_DEL);
        }
        if(foodVariety.getState()==ConstantData.FOOD_STATE_DISABLE){

        }
        return null;
    }

    @Override
    public WoniuPageDto searchByType(Integer typeId) {
        Page<FoodVariety> page = this.lambdaQuery()
                .eq(Objects.nonNull(typeId), FoodVariety::getTypeId, typeId)
                .eq(FoodVariety::getState,1)
                .page(new Page<>(1, 20));
        List<FoodForUserDto> dtos = new ArrayList<>();
        for (FoodVariety food : page.getRecords()) {
            FoodForUserDto dto = BeanUtil.toBean(food, FoodForUserDto.class);
            dto.setCount(1); //月销量
            dtos.add(dto);
        }
        WoniuPageDto pageDto = BeanUtil.toBean(page, WoniuPageDto.class);
        pageDto.setList(dtos);
        return pageDto;
    }
}
